package com.foreknow.service.impl;

import com.foreknow.dao.impl.BusinessDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Business;
import com.foreknow.service.BusinessService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-05 15:59
 */
public class BusinessServiceImpl implements BusinessService {

    private BusinessDaoImpl dao = new BusinessDaoImpl();

    @Override
    public List<Object> showAll() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        dao.setConnection(conn);
        try {
            List<Object> queryAll = dao.getQueryAll();
            return queryAll;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public boolean add(Business business) {
        DBManager dbManager = DBManager.getInstance();
        Connection con = dbManager.getConnection();
        dao.setConnection(con);

        boolean save = false;
        try {
            save = dao.save(business);
            return save;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return save;
    }

    @Override
    public boolean del(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection con = dbManager.getConnection();
        dao.setConnection(con);
        try {
            boolean delete = dao.delete(id);
            return delete;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Object getQueryById(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        dao.setConnection(conn);

        try {
            Object queryById = dao.getQueryById(id);


            return queryById;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean Update(Business business) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        dao.setConnection(conn);

        try {
            boolean updatedao = dao.Updatedao(business);

            return updatedao;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Object> showName(String name) {
        DBManager instance = DBManager.getInstance();
        Connection conn = instance.getConnection();
        dao.setConnection(conn);
        try {
            List<Object> nameAll = dao.getNameAll(name);
            return nameAll;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


}
